package co.edu.udea.quejas.DAO.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import co.edu.udea.quejas.DAO.PersonaDAO;
import co.edu.udea.quejas.DTO.Persona;
import co.edu.udea.quejas.exception.IWDaoException;

public class PersonaDAOHibernate extends HibernateDaoSupport implements PersonaDAO {


	@Override
	public Persona insertar(Persona persona)   {
		Transaction tx=null;
		Session session= null;
		try{		
			
			session = getSession();
			tx= session.beginTransaction();
			session.save(persona);
			tx.commit();
			System.out.println("Ingresado");
			}catch(HibernateException e){
				System.out.println(e);
			}finally {
					session.close();
			
		}
		return persona;
	}

	@Override
	public Persona modificar(Persona persona) throws IWDaoException {
		Transaction tx=null;
		Session session=null;
		try{
			session= getSession();			
			tx= session.beginTransaction();
			session.update(persona);
			tx.commit();
			
			}catch(HibernateException e){
				throw new IWDaoException(e);
			}finally {
			session.close();
			
		}
		return persona;
	}

	@Override
	public void eliminar(Persona persona) throws IWDaoException {
		Transaction tx=null;
		Session session=null;
		try{
			session= getSession();			
			tx= session.beginTransaction();
			//eliminar logicamente- HABLAR CON DANIEL
			session.update(persona);
			tx.commit();
			
			}catch(HibernateException e){
				throw new IWDaoException(e);
			}finally {
			session.close();
			
		}
	}
	
	@Override
	public void eliminarDef(Persona persona) throws IWDaoException {
		Transaction tx=null;
		Session session=null;
		try{
			session= getSession();			
			tx= session.beginTransaction();
			//eliminar logicamente- HABLAR CON DANIEL
			session.delete(persona);
			tx.commit();
			
			}catch(HibernateException e){
				throw new IWDaoException(e);
			}finally {
			session.close();
		}
		
	}

	@Override
	public List<Persona> obtener() throws IWDaoException {
		Session session =null;
		List<Persona> clientes = new ArrayList<Persona>();
		try{
		session=  this.getHibernateTemplate().getSessionFactory().getCurrentSession();
		
		Criteria criteria = session.createCriteria(Persona.class);
		clientes = criteria.list();
		}catch(HibernateException e){
			throw new IWDaoException(e);
		}finally{
			
			session.close();
		}
		return clientes;
	}

	@Override
	public Persona obtener(String cedula) throws IWDaoException {
		Persona persona = null;
		Session session =null;
		try{
			session= getSession();
		persona= (Persona) session.get(Persona.class, cedula);	
		
		}catch(HibernateException e){
			throw new IWDaoException(e);
		}finally{
			session.close();
		}
		return persona;

	}
	

}
